﻿/// <reference path="jquery-1.5.1-vsdoc.js" />
$(function () {
    $('.edit-bar .buttons .btn').click(function () {
        $(this).toggleClass('active');
    });

    //    $(".new-product-edit-wrapper").find('#Name').change(function () {
    //        $('.lab-product-name').text($(this).val());
    //    });

    $("#edit-product").click(function () {
        $(".new-product-edit-wrapper").slideToggle("fast");
    });

    $('.new-product-edit-wrapper.ajax').submit(function (e) {
        var $form = $(this);
        if ($form.valid()) {
            e.preventDefault();
            $.ajax({
                url: $(this).attr('action')
                , data: $(this).serialize()
                , type: 'put'
                , success: function (html) {
                    //$(html).prependTo('.product-list');
                    //$($form).trigger('reset');
                    // update labels
                    $('.lab-product-name').text($form.find('#Name').val());
                    $form.trigger('close');
                }
                , error: function (xhr) {
                    //jQuery.validator.unobtrusive.parse();
                    $($form).html(xhr.responseText);
                }
            });
            return false;
        }
    });

    $('.new-product-edit-wrapper').on('close', function () {
        $('#edit-product').toggleClass('active');
        $(".new-product-edit-wrapper").slideToggle("fast");
    });

    $('.new-product-edit-wrapper').on('reset', function () {
        $(this).trigger('close');
    });

    $('.switch-checkbox').click(function () {
        var checked = $('input[type=checkbox]', this).is(':checked');
        $('input[type=checkbox]', this).attr('checked', (!checked ? 'checked' : null));
        $('input[type=checkbox]', this).val(checked);
        $('input[type=checkbox]', this).prev().val(checked);
    });

    $('.new-story-edit-wrapper').on('close', function () {
        $('#add-story').toggleClass('active');
        $(".new-story-edit-wrapper").slideToggle("fast");
    });

    $('.new-story-edit-wrapper').on('reset', function () {
        $(this).trigger('close');
    });

    $('.new-story-edit-wrapper.ajax').submit(function (e) {
        var $form = $(this);
        if ($form.valid()) {
            e.preventDefault();
            $.ajax({
                url: $(this).attr('action')
                , data: $(this).serialize()
                , type: $form.attr('method')
                , success: function (html) {
                    $(html).prependTo('.story-list');
                    // update labels
                    //$('.lab-product-name').text($form.find('#Name').val());
                    $form.trigger('reset');
                    //$form.trigger('close');
                }
                , error: function (xhr) {
                    //jQuery.validator.unobtrusive.parse();
                    $form.html(xhr.responseText);
                }
            });
            return false;
        }
    });
});